IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_CustomerQuestionnaire]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_CustomerQuestionnaire]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Customer]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Customer]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Question]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Question]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_QuestionnaireSecurityAddIn]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_QuestionnaireSecurityAddIn]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_QuestionnaireLanguage]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_QuestionnaireLanguage]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_QuestionnairePopup]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_QuestionnairePopup]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_QuestionnaireEntryQuota]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_QuestionnaireEntryQuota]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Questionnaire]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Questionnaire]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Folder]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Folder]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Library]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Library]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Panel]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Panel]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ConnectorAttribute]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ConnectorAttribute]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ConnectorSourceType]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ConnectorSourceType]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Connector]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Connector]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_Validator]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_Validator]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_AnswerType]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_AnswerType]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_SelectionMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_SelectionMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_SecurityAddIn]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_SecurityAddIn]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ChangeMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ChangeMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_MultiLanguageMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_MultiLanguageMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_PrivacyAction]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_PrivacyAction]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ProgressDisplayMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ProgressDisplayMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ResumeMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ResumeMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_UnAuthentifiedUserAction]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_UnAuthentifiedUserAction]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_FolderSortType]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_FolderSortType]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_LayoutMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_LayoutMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_UserMappingType]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_UserMappingType]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ConnectorUpdateMode]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ConnectorUpdateMode]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_ItemCollectionLayout]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_ItemCollectionLayout]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[survey_MultiLanguage]') AND type in (N'U'))
	DROP TABLE [dbo].[survey_MultiLanguage]
GO

-- MultiLanguage
CREATE TABLE [dbo].[survey_MultiLanguage]
(
	[LanguageCode] [varchar](32) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[IsReverseLanguage] [bit] NULL CONSTRAINT [DF_survey_MultiLanguage_Reverse]  DEFAULT ((0))
)
GO
ALTER TABLE [dbo].[survey_MultiLanguage] ADD CONSTRAINT [PK_survey_MultiLanguage] PRIMARY KEY CLUSTERED 
(
	[LanguageCode] ASC
)
GO
ALTER TABLE [dbo].[survey_MultiLanguage] ADD CONSTRAINT [UK_survey_MultiLanguage_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- ItemCollectionLayout
CREATE TABLE [dbo].[survey_ItemCollectionLayout]
(
	[ItemCollectionLayoutID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_ItemCollectionLayout] ADD CONSTRAINT [PK_survey_ItemCollectionLayout] PRIMARY KEY CLUSTERED 
(
	[ItemCollectionLayoutID] ASC
)
GO
ALTER TABLE [dbo].[survey_ItemCollectionLayout] ADD CONSTRAINT [UK_survey_ItemCollectionLayout_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- ConnectorUpdateMode
CREATE TABLE [dbo].[survey_ConnectorUpdateMode]
(
	[ConnectorUpdateModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_ConnectorUpdateMode] ADD CONSTRAINT [PK_survey_ConnectorUpdateMode] PRIMARY KEY CLUSTERED 
(
	[ConnectorUpdateModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_ConnectorUpdateMode] ADD CONSTRAINT [UK_survey_ConnectorUpdateMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- UserMappingType
CREATE TABLE [dbo].[survey_UserMappingType]
(
	[UserMappingTypeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_UserMappingType] ADD CONSTRAINT [PK_survey_UserMappingType] PRIMARY KEY CLUSTERED 
(
	[UserMappingTypeID] ASC
)
GO
ALTER TABLE [dbo].[survey_UserMappingType] ADD CONSTRAINT [UK_survey_UserMappingType_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- LayoutMode
CREATE TABLE [dbo].[survey_LayoutMode]
(
	[LayoutModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_LayoutMode] ADD CONSTRAINT [PK_survey_LayoutMode] PRIMARY KEY CLUSTERED 
(
	[LayoutModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_LayoutMode] ADD CONSTRAINT [UK_survey_LayoutMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- FolderSortType
CREATE TABLE [dbo].[survey_FolderSortType]
(
	[FolderSortTypeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_FolderSortType] ADD CONSTRAINT [PK_survey_FolderSortType] PRIMARY KEY CLUSTERED 
(
	[FolderSortTypeID] ASC
)
GO
ALTER TABLE [dbo].[survey_FolderSortType] ADD CONSTRAINT [UK_survey_FolderSortType_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- UnAuthentifiedUserAction
CREATE TABLE [dbo].[survey_UnAuthentifiedUserAction]
(
	[UnAuthentifiedUserActionID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_UnAuthentifiedUserAction] ADD CONSTRAINT [PK_survey_UnAuthentifiedUserAction] PRIMARY KEY CLUSTERED 
(
	[UnAuthentifiedUserActionID] ASC
)
GO
ALTER TABLE [dbo].[survey_UnAuthentifiedUserAction] ADD CONSTRAINT [UK_survey_UnAuthentifiedUserAction_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- ResumeMode
CREATE TABLE [dbo].[survey_ResumeMode]
(
	[ResumeModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_ResumeMode] ADD CONSTRAINT [PK_survey_ResumeMode] PRIMARY KEY CLUSTERED 
(
	[ResumeModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_ResumeMode] ADD CONSTRAINT [UK_survey_ResumeMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- ProgressDisplayMode
CREATE TABLE [dbo].[survey_ProgressDisplayMode]
(
	[ProgressDisplayModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_ProgressDisplayMode] ADD CONSTRAINT [PK_survey_ProgressDisplayMode] PRIMARY KEY CLUSTERED 
(
	[ProgressDisplayModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_ProgressDisplayMode] ADD CONSTRAINT [UK_survey_ProgressDisplayMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- PrivacyAction
CREATE TABLE [dbo].[survey_PrivacyAction]
(
	[PrivacyActionID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_PrivacyAction] ADD CONSTRAINT [PK_survey_PrivacyAction] PRIMARY KEY CLUSTERED 
(
	[PrivacyActionID] ASC
)
GO
ALTER TABLE [dbo].[survey_PrivacyAction] ADD CONSTRAINT [UK_survey_PrivacyAction_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- MultiLanguageMode
CREATE TABLE [dbo].[survey_MultiLanguageMode]
(
	[MultiLanguageModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_MultiLanguageMode] ADD CONSTRAINT [PK_survey_MultiLanguageMode] PRIMARY KEY CLUSTERED 
(
	[MultiLanguageModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_MultiLanguageMode] ADD CONSTRAINT [UK_survey_MultiLanguageMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- ChangeMode
CREATE TABLE [dbo].[survey_ChangeMode]
(
	[ChangeModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_ChangeMode] ADD CONSTRAINT [PK_survey_ChangeMode] PRIMARY KEY CLUSTERED 
(
	[ChangeModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_ChangeMode] ADD CONSTRAINT [UK_survey_ChangeMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- SecurityAddIn
CREATE TABLE [dbo].[survey_SecurityAddIn]
(
	[SecurityAddInID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[BuiltIn] [bit] NULL CONSTRAINT [DF_survey_SecurityAddIn_BuiltIn]  DEFAULT ((0)),
	[TypeNameSpace] [varchar](256) NOT NULL,
	[TypeAssembly] [varchar](128) NULL,
	[TypeMode] [int] NULL
)
GO
ALTER TABLE [dbo].[survey_SecurityAddIn] ADD CONSTRAINT [PK_survey_SecurityAddIn] PRIMARY KEY CLUSTERED 
(
	[SecurityAddInID] ASC
)
GO

-- SelectionMode
CREATE TABLE [dbo].[survey_SelectionMode]
(
	[SelectionModeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[Description] [nvarchar](max) NULL,
	[TypeNameSpace] [varchar](256) NOT NULL,
	[TypeAssembly] [varchar](128) NULL,
	[TypeMode] [int] NULL,
	[TypeIcon] [nvarchar](128) NULL CONSTRAINT [DF_survey_SelectionMode_TypeIcon]  DEFAULT (N'customquestion.gif')
)
GO
ALTER TABLE [dbo].[survey_SelectionMode] ADD CONSTRAINT [PK_survey_SelectionMode] PRIMARY KEY CLUSTERED 
(
	[SelectionModeID] ASC
)
GO
ALTER TABLE [dbo].[survey_SelectionMode] ADD CONSTRAINT [UK_survey_SelectionMode_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)
GO

-- AnswerType
CREATE TABLE [dbo].[survey_AnswerType]
(
	[AnswerTypeID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[TypeNameSpace] [varchar](256) NOT NULL,
	[TypeAssembly] [varchar](128) NULL,
	[TypeMode] [int] NULL,
	[TypeIcon] [nvarchar](128) NULL CONSTRAINT [DF_survey_AnswerType_TypeIcon]  DEFAULT (N'customquestion.gif'),
	[FieldWidth] [int] NULL CONSTRAINT [DF_survey_AnswerType_FieldWidth]  DEFAULT ((0)),
	[FieldHeight] [int] NULL CONSTRAINT [DF_survey_AnswerType_FieldHeight]  DEFAULT ((0)),
	[XMLDataSource] [varchar](256) NULL,
	[PublicFieldResults] [bit] NULL CONSTRAINT [DF_survey_AnswerType_PublicFieldResults]  DEFAULT ((0)),
	[JavascriptFunctionName] [varchar](256) NULL,
	[JavascriptCode] [varchar](max) NULL,
	[JavascriptErrorMessage] [varchar](max) NULL,
	[DataSource] [nvarchar](max) NULL,
	[ItemCollectionLayoutID] [int] NULL,
	[BuiltIn] [bit] NULL CONSTRAINT [DF_survey_AnswerType_BuiltIn]  DEFAULT ((0))
)
GO
ALTER TABLE [dbo].[survey_AnswerType] ADD CONSTRAINT [PK_survey_AnswerType] PRIMARY KEY CLUSTERED 
(
	[AnswerTypeID] ASC
)
GO
ALTER TABLE [dbo].[survey_AnswerType] WITH NOCHECK ADD CONSTRAINT [FK_survey_AnswerType_survey_ItemCollectionLayout] FOREIGN KEY([ItemCollectionLayoutID]) REFERENCES [dbo].[survey_ItemCollectionLayout] ([ItemCollectionLayoutID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Validator
CREATE TABLE [dbo].[survey_Validator]
(
	[ValidatorID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[TypeNameSpace] [varchar](256) NOT NULL,
	[TypeAssembly] [varchar](128) NULL,
	[TypeMode] [int] NULL
)
GO
ALTER TABLE [dbo].[survey_Validator] ADD CONSTRAINT [PK_survey_Validator] PRIMARY KEY CLUSTERED 
(
	[ValidatorID] ASC
)
GO

-- Connector
CREATE TABLE [dbo].[survey_Connector]
(
	[ConnectorID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](64) NOT NULL,
	[TypeNameSpace] [varchar](256) NOT NULL,
	[TypeAssembly] [varchar](128) NULL,
	[TypeMode] [int] NULL
)
GO
ALTER TABLE [dbo].[survey_Connector] ADD CONSTRAINT [PK_survey_Connector] PRIMARY KEY CLUSTERED 
(
	[ConnectorID] ASC
)
GO

-- ConnectorSourceType
CREATE TABLE [dbo].[survey_ConnectorSourceType]
(
	[ConnectorID] [int] NOT NULL,
	[SourceTypeName] [nvarchar](256) NOT NULL,
	[AnswerTypeID] [int] NOT NULL,
	[ValidatorID] [int] NULL
)
GO
ALTER TABLE [dbo].[survey_ConnectorSourceType] ADD CONSTRAINT [PK_survey_ConnectorSourceType] PRIMARY KEY CLUSTERED 
(
	[ConnectorID] ASC,
	[SourceTypeName] ASC
)
GO
ALTER TABLE [dbo].[survey_ConnectorSourceType] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorSourceType_survey_Connector] FOREIGN KEY([ConnectorID]) REFERENCES [dbo].[survey_Connector] ([ConnectorID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_ConnectorSourceType] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorSourceType_survey_AnswerType] FOREIGN KEY([AnswerTypeID]) REFERENCES [dbo].[survey_AnswerType] ([AnswerTypeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_ConnectorSourceType] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorSourceType_survey_Validator] FOREIGN KEY([ValidatorID]) REFERENCES [dbo].[survey_Validator] ([ValidatorID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- ConnectorAttribute
CREATE TABLE [dbo].[survey_ConnectorAttribute]
(
	[ConnectorID] [int] NOT NULL,
	[AttributeName] [nvarchar](256) NOT NULL,
	[Description] [nvarchar](max) NULL,
	[AnswerTypeID] [int] NULL,
	[ValidatorID] [int] NULL,
	[MaxLength] [int] NULL,
	[Mandatory] [bit] NOT NULL CONSTRAINT [DF_survey_ConnectorAttribute_Mandatory]  DEFAULT ((0)),
	[AddOrder] [int] NULL
)
GO
ALTER TABLE [dbo].[survey_ConnectorAttribute] ADD CONSTRAINT [PK_survey_ConnectorAttribute] PRIMARY KEY CLUSTERED 
(
	[ConnectorID] ASC,
	[AttributeName] ASC
)
GO
ALTER TABLE [dbo].[survey_ConnectorAttribute] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorAttribute_survey_Connector] FOREIGN KEY([ConnectorID]) REFERENCES [dbo].[survey_Connector] ([ConnectorID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_ConnectorAttribute] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorAttribute_survey_AnswerType] FOREIGN KEY([AnswerTypeID]) REFERENCES [dbo].[survey_AnswerType] ([AnswerTypeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_ConnectorAttribute] WITH NOCHECK ADD CONSTRAINT [FK_survey_ConnectorAttribute_survey_Validator] FOREIGN KEY([ValidatorID]) REFERENCES [dbo].[survey_Validator] ([ValidatorID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Panel
CREATE TABLE [dbo].[survey_Panel]
(
	[PanelID] [int] IDENTITY(1,1) NOT NULL,
	[PortalID] [int] NOT NULL,
	[ConnectorID] [int] NULL,
	[UserMappingTypeID] [int] NULL,
	[MappingAnswerID] [int] NULL,
	[PanelName] [nvarchar](256) NOT NULL,
	[ConnectorUpdateModeID] [int] NULL CONSTRAINT [DF_survey_Panel_ConnectorUpdateModeID]  DEFAULT ((1)),
	[SourceSyncDelay] [int] NULL CONSTRAINT [DF_survey_Panel_SourceSyncDelay]  DEFAULT ((0)),
	[SyncOnAppRestart] [bit] NULL
)
GO
ALTER TABLE [dbo].[survey_Panel] ADD CONSTRAINT [PK_survey_Panel] PRIMARY KEY CLUSTERED 
(
	[PanelID] ASC
)
GO
ALTER TABLE [dbo].[survey_Panel] WITH NOCHECK ADD CONSTRAINT [FK_survey_Panel_dnn_Portals] FOREIGN KEY([PortalID]) REFERENCES [dbo].[dnn_Portals] ([PortalID])
GO
ALTER TABLE [dbo].[survey_Panel] WITH NOCHECK ADD CONSTRAINT [FK_survey_Panel_survey_Connector] FOREIGN KEY([ConnectorID]) REFERENCES [dbo].[survey_Connector] ([ConnectorID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Panel] WITH NOCHECK ADD CONSTRAINT [FK_survey_Panel_survey_UserMappingType] FOREIGN KEY([UserMappingTypeID]) REFERENCES [dbo].[survey_UserMappingType] ([UserMappingTypeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Panel] WITH NOCHECK ADD CONSTRAINT [FK_survey_Panel_survey_ConnectorUpdateMode] FOREIGN KEY([ConnectorUpdateModeID]) REFERENCES [dbo].[survey_ConnectorUpdateMode] ([ConnectorUpdateModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Library
CREATE TABLE [dbo].[survey_Library]
(
	[LibraryID] [int] IDENTITY(1,1) NOT NULL,
	[PortalID] [int] NOT NULL,
	[LibraryName] [nvarchar](256) NOT NULL
)
GO
ALTER TABLE [dbo].[survey_Library] ADD CONSTRAINT [PK_survey_Library] PRIMARY KEY CLUSTERED 
(
	[LibraryID] ASC
)
GO
ALTER TABLE [dbo].[survey_Library] WITH NOCHECK ADD CONSTRAINT [FK_survey_Library_dnn_Portals] FOREIGN KEY([PortalID]) REFERENCES [dbo].[dnn_Portals] ([PortalID])
GO

-- Folder
CREATE TABLE [dbo].[survey_Folder]
(
	[FolderID] [int] IDENTITY(1,1) NOT NULL,
	[PortalID] [int] NOT NULL,
	[ParentFolderID] [int] NULL,
	[FolderSortTypeID] [int] NULL,
	[FolderName] [nvarchar](256) NOT NULL
)
GO
ALTER TABLE [dbo].[survey_Folder] ADD CONSTRAINT [PK_survey_Folder] PRIMARY KEY CLUSTERED 
(
	[FolderID] ASC
)
GO
ALTER TABLE [dbo].[survey_Folder] WITH NOCHECK ADD CONSTRAINT [FK_survey_Folder_dnn_Portals] FOREIGN KEY([PortalID]) REFERENCES [dbo].[dnn_Portals] ([PortalID])
GO
ALTER TABLE [dbo].[survey_Folder] WITH NOCHECK ADD CONSTRAINT [FK_survey_Folder_survey_Folder] FOREIGN KEY([ParentFolderID]) REFERENCES [dbo].[survey_Folder] ([FolderID])
GO
ALTER TABLE [dbo].[survey_Folder] WITH NOCHECK ADD CONSTRAINT [FK_survey_Folder_survey_FolderSortType] FOREIGN KEY([FolderSortTypeID]) REFERENCES [dbo].[survey_FolderSortType] ([FolderSortTypeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Questionnaire
CREATE TABLE [dbo].[survey_Questionnaire]
(
	[QuestionnaireID] [int] IDENTITY(1,1) NOT NULL,
	[QuestionnaireUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_survey_Questionnaire_QuestionnaireUID]  DEFAULT (newid()),
	[PortalID] [int] NOT NULL,
	[FolderID] [int] NULL,
	[ProgressDisplayModeID] [int] NULL CONSTRAINT [DF_survey_Questionnaire_ProgressDisplayModeID]  DEFAULT ((2)),
	[ResumeModeID] [int] NULL CONSTRAINT [DF_survey_Questionnaire_ResumeModeID]  DEFAULT ((1)),
	[ChangeModeID] [int] NULL CONSTRAINT [DF_survey_Questionnaire_ChangeModeID]  DEFAULT ((1)),
	[PrivacyActionID] [int] NULL CONSTRAINT [DF_survey_Questionnaire_PrivacyActionID]  DEFAULT ((1)),
	[CreationDate] [datetime] NOT NULL CONSTRAINT [DF_survey_Questionnaire_CreationDate]  DEFAULT (getdate()),
	[LastEntryDate] [datetime] NULL,
	[OpenDate] [datetime] NULL,
	[CloseDate] [datetime] NULL,
	[Title] [nvarchar](256) NULL,
	[RedirectionURL] [varchar](1024) NULL,
	[ThankYouMessage] [nvarchar](max) NULL,
	[AccessPassword] [nvarchar](256) NULL,
	[TotalPageNumber] [int] NULL CONSTRAINT [DF_survey_Questionnaire_TotalPageNumber]  DEFAULT ((1)),
	[DisplayTimes] [int] NOT NULL CONSTRAINT [DF_survey_Questionnaire_PollBoxDisplayTimes]  DEFAULT ((0)),
	[ResultsDisplayTimes] [int] NOT NULL CONSTRAINT [DF_survey_Questionnaire_ResultsDisplayTimes]  DEFAULT ((0)),
	[NavigationEnabled] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_NavigationEnabled]  DEFAULT ((0)),
	[Archive] [bit] NOT NULL CONSTRAINT [DF_survey_Questionnaire_Archive]  DEFAULT ((0)),
	[Activated] [bit] NOT NULL CONSTRAINT [DF_survey_Questionnaire_Activated]  DEFAULT ((0)),
	[Scored] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_Scored]  DEFAULT ((0)),
	[IPExpires] [int] NULL CONSTRAINT [DF_survey_Questionnaire_IPExpires]  DEFAULT ((1440)),
	[CookieExpires] [int] NULL CONSTRAINT [DF_survey_Questionnaire_CookieExpires]  DEFAULT ((1440)),
	[OnlyInvited] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_OnyInvited]  DEFAULT ((0)),
	[UnAuthentifiedUserActionID] [int] NULL,
	[AllowMultipleUserNameSubmissions] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_AllowMultipleUserNameSubmissions]  DEFAULT ((0)),
	[QuestionNumberingDisabled] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_DisableQuestionNumbering]  DEFAULT ((0)),
	[AllowMultipleNQuestionnaireSubmissions] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_AllowMultipleNQuestionnaireSubmissions]  DEFAULT ((0)),
	[MultiLanguageModeID] [int] NULL CONSTRAINT [DF_survey_Questionnaire_MultiLanguageModeID]  DEFAULT ((0)),
	[MultiLanguageVariable] [nvarchar](64) NULL,
	[AnonymousRespondents] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_AnonymousRespondent]  DEFAULT ((0)),
	[HeaderImageURL] [varchar](1024) NULL,
	[ShowAnswerSummary] [bit] NULL CONSTRAINT [DF_survey_Questionnaire_ShowAnswerSummary]  DEFAULT ((0)),
	[Description] [nvarchar](max) NULL,
	[MultiLangIntroduction] [nvarchar](max) NULL,
	[FriendlyName] [varchar](32) NULL
)
GO
ALTER TABLE [dbo].[survey_Questionnaire] ADD CONSTRAINT [PK_survey_Questionnaire] PRIMARY KEY CLUSTERED 
(
	[QuestionnaireID] ASC
)
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_dnn_Portals] FOREIGN KEY([PortalID]) REFERENCES [dbo].[dnn_Portals] ([PortalID])
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_Folder] FOREIGN KEY([FolderID]) REFERENCES [dbo].[survey_Folder] ([FolderID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_ChangeMode] FOREIGN KEY([ChangeModeID]) REFERENCES [dbo].[survey_ChangeMode] ([ChangeModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_MultiLanguageMode] FOREIGN KEY([MultiLanguageModeID]) REFERENCES [dbo].[survey_MultiLanguageMode] ([MultiLanguageModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_PrivacyAction] FOREIGN KEY([PrivacyActionID]) REFERENCES [dbo].[survey_PrivacyAction] ([PrivacyActionID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_ProgressDisplayMode] FOREIGN KEY([ProgressDisplayModeID]) REFERENCES [dbo].[survey_ProgressDisplayMode] ([ProgressDisplayModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_ResumeMode] FOREIGN KEY([ResumeModeID]) REFERENCES [dbo].[survey_ResumeMode] ([ResumeModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Questionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_Questionnaire_survey_UnAuthentifiedUserAction] FOREIGN KEY([UnAuthentifiedUserActionID]) REFERENCES [dbo].[survey_UnAuthentifiedUserAction] ([UnAuthentifiedUserActionID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- QuestionnaireEntryQuota
CREATE TABLE [dbo].[survey_QuestionnaireEntryQuota]
(
	[QuestionnaireID] [int] NOT NULL,
	[MaxEntries] [int] NULL CONSTRAINT [DF_survey_QuestionnaireEntryQuota_MaxEntries]  DEFAULT ((1)),
	[EntryCount] [int] NULL CONSTRAINT [DF_survey_QuestionnaireEntryQuota_EntryCount]  DEFAULT ((0)),
	[MaxReachedMessage] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireEntryQuota] ADD CONSTRAINT [PK_survey_QuestionnaireEntryQuota] PRIMARY KEY CLUSTERED 
(
	[QuestionnaireID] ASC
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireEntryQuota] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnaireEntryQuota_survey_Questionnaire] FOREIGN KEY([QuestionnaireID]) REFERENCES [dbo].[survey_Questionnaire] ([QuestionnaireID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- QuestionnairePopup
CREATE TABLE [dbo].[survey_QuestionnairePopup]
(
	[QuestionnaireID] [int] NOT NULL,
	[PopFrequency] [int] NULL CONSTRAINT [DF_survey_QuestionnairePopup_PopFrequency]  DEFAULT ((100)),
	[NewWindow] [bit] NULL CONSTRAINT [DF_survey_QuestionnairePopup_NewWindow]  DEFAULT ((0)),
	[Title] [nvarchar](256) NULL,
	[LinkText] [nvarchar](256) NULL,
	[Message] [nvarchar](max) NULL
)
GO
ALTER TABLE [dbo].[survey_QuestionnairePopup] ADD CONSTRAINT [PK_survey_QuestionnairePopup] PRIMARY KEY CLUSTERED 
(
	[QuestionnaireID] ASC
)
GO
ALTER TABLE [dbo].[survey_QuestionnairePopup] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnairePopup_survey_Questionnaire] FOREIGN KEY([QuestionnaireID]) REFERENCES [dbo].[survey_Questionnaire] ([QuestionnaireID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- QuestionnaireLanguage
CREATE TABLE [dbo].[survey_QuestionnaireLanguage]
(
	[QuestionnaireID] [int] NOT NULL,
	[LanguageCode] [varchar](32) NOT NULL,
	[DefaultLanguage] [bit] NULL CONSTRAINT [DF_survey_QuestionnaireLanguage_DefaultLanguage]  DEFAULT ((0))
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireLanguage] ADD CONSTRAINT [PK_survey_QuestionnaireLanguage] PRIMARY KEY CLUSTERED 
(
	[QuestionnaireID] ASC,
	[LanguageCode] ASC
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireLanguage] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnaireLanguage_survey_Questionnaire] FOREIGN KEY([QuestionnaireID]) REFERENCES [dbo].[survey_Questionnaire] ([QuestionnaireID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_QuestionnaireLanguage] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnaireLanguage_survey_MultiLanguage] FOREIGN KEY([LanguageCode]) REFERENCES [dbo].[survey_MultiLanguage] ([LanguageCode])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- QuestionnaireSecurityAddIn
CREATE TABLE [dbo].[survey_QuestionnaireSecurityAddIn]
(
	[QuestionnaireID] [int] NOT NULL,
	[SecurityAddinID] [int] NOT NULL,
	[SortOrder] [int] NULL CONSTRAINT [DF_survey_QuestionnaireSecurity_SortOrder]  DEFAULT ((1)),
	[Disabled] [bit] NULL CONSTRAINT [DF_survey_QuestionnaireSecurity_Disabled]  DEFAULT ((0)),
	[IsKeyProvider] [bit] NULL CONSTRAINT [DF_survey_QuestionnaireSecurity_IsKeyProvider]  DEFAULT ((0)),
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireSecurityAddIn] ADD CONSTRAINT [PK_survey_QuestionnaireSecurityAddIn] PRIMARY KEY CLUSTERED 
(
	[QuestionnaireID] ASC,
	[SecurityAddinID] ASC
)
GO
ALTER TABLE [dbo].[survey_QuestionnaireSecurityAddIn] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnaireSecurityAddIn_survey_Questionnaire] FOREIGN KEY([QuestionnaireID]) REFERENCES [dbo].[survey_Questionnaire] ([QuestionnaireID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_QuestionnaireSecurityAddIn] WITH NOCHECK ADD CONSTRAINT [FK_survey_QuestionnaireSecurityAddIn_survey_SecurityAddIn] FOREIGN KEY([SecurityAddinID]) REFERENCES [dbo].[survey_SecurityAddIn] ([SecurityAddinID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Question
CREATE TABLE [dbo].[survey_Question]
(
	[QuestionID] [int] IDENTITY(1,1) NOT NULL,
	[PortalID] [int] NOT NULL,
	[ParentQuestionID] [int] NULL,
	[PanelID] [int] NULL,
	[LibraryID] [int] NULL,
	[LayoutModeID] [int] NULL,
	[SelectionModeID] [int] NULL CONSTRAINT [DF_survey_Question_iQuestionLayoutID]  DEFAULT ((0)),
	[ItemLayoutID] [int] NULL CONSTRAINT [DF_survey_Question_ItemLayoutID]  DEFAULT ((1)),
	[ColumnsNumber] [int] NULL CONSTRAINT [DF_survey_Question_ColumnsNumber]  DEFAULT ((0)),
	[QuestionText] [nvarchar](max) NULL,
	[ChildDisplayOrder] [int] NULL,
	[MinSelectionRequired] [int] NULL CONSTRAINT [DF_survey_Question_MinSelectionRequired]  DEFAULT ((0)),
	[MaxSelectionAllowed] [int] NULL CONSTRAINT [DF_survey_Question_MaxSelectionAllowed]  DEFAULT ((0)),
	[RatingEnabled] [bit] NOT NULL CONSTRAINT [DF_survey_Question_RatingEnabled]  DEFAULT ((0)),
	[RandomizeAnswers] [bit] NOT NULL CONSTRAINT [DF_survey_Question_RandomizeAnswers]  DEFAULT ((0)),
	[PipeAlias] [nvarchar](64) NULL,
	[ReportingAlias] [nvarchar](64) NULL,
	[IsExtendedFilter] [bit] NULL CONSTRAINT [DF_survey_Question_IsExtendedFilter]  DEFAULT ((0))
)
GO
ALTER TABLE [dbo].[survey_Question] ADD CONSTRAINT [PK_survey_Question] PRIMARY KEY CLUSTERED 
(
	[QuestionID] ASC
)
GO
ALTER TABLE [dbo].[survey_Question] WITH NOCHECK ADD CONSTRAINT [FK_survey_Question_dnn_Portals] FOREIGN KEY([PortalID]) REFERENCES [dbo].[dnn_Portals] ([PortalID])
GO
ALTER TABLE [dbo].[survey_Question] WITH NOCHECK ADD CONSTRAINT [FK_survey_Question_survey_Panel] FOREIGN KEY([PanelID]) REFERENCES [dbo].[survey_Panel] ([PanelID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Question] WITH NOCHECK ADD CONSTRAINT [FK_survey_Question_survey_Library] FOREIGN KEY([LibraryID]) REFERENCES [dbo].[survey_Library] ([LibraryID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Question] WITH NOCHECK ADD CONSTRAINT [FK_survey_Question_survey_LayoutMode] FOREIGN KEY([LayoutModeID]) REFERENCES [dbo].[survey_LayoutMode] ([LayoutModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_Question] WITH NOCHECK ADD CONSTRAINT [FK_survey_Question_survey_SelectionMode] FOREIGN KEY([SelectionModeID]) REFERENCES [dbo].[survey_SelectionMode] ([SelectionModeID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- Customer
CREATE TABLE [dbo].[survey_Customer]
(
	[CustomerID] [int] NOT NULL,
	[BirthDate] [datetime] NULL
)
GO
ALTER TABLE [dbo].[survey_Customer] ADD CONSTRAINT [PK_survey_Customer] PRIMARY KEY CLUSTERED 
(
	[CustomerID] ASC
)
GO
ALTER TABLE [dbo].[survey_Customer] WITH NOCHECK ADD CONSTRAINT [FK_survey_Customer_dnn_Users] FOREIGN KEY([CustomerID]) REFERENCES [dbo].[dnn_Users] ([UserID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

-- CustomerQuestionnaire
CREATE TABLE [dbo].[survey_CustomerQuestionnaire]
(
	[CustomerID] [int] NOT NULL,
	[QuestionnaireID] [int] NOT NULL
)
GO
ALTER TABLE [dbo].[survey_CustomerQuestionnaire] ADD CONSTRAINT [PK_survey_CustomerQuestionnaire] PRIMARY KEY CLUSTERED 
(
	[CustomerID] ASC,
	[QuestionnaireID] ASC
)
GO
ALTER TABLE [dbo].[survey_CustomerQuestionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_CustomerQuestionnaire_survey_Customer] FOREIGN KEY([CustomerID]) REFERENCES [dbo].[survey_Customer] ([CustomerID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[survey_CustomerQuestionnaire] WITH NOCHECK ADD CONSTRAINT [FK_survey_CustomerQuestionnaire_survey_Questionnaire] FOREIGN KEY([QuestionnaireID]) REFERENCES [dbo].[survey_Questionnaire] ([QuestionnaireID])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
